from datetime import datetime, date
from sqlalchemy import String, DateTime, Integer, SmallInteger, JSON, Boolean, Date, Index
from sqlalchemy.orm import Mapped, mapped_column
from config.database import Base


class SpeechClientUserDO(Base):
    """
    语音转写客户端用户表
    """
    __tablename__ = 'speech_client_user'

    client_user_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, comment='客户端用户ID')
    username: Mapped[str] = mapped_column(String(50), nullable=False, comment='用户名')
    display_name: Mapped[str] = mapped_column(String(100), nullable=True, comment='显示名称')
    email: Mapped[str] = mapped_column(String(100), nullable=True, comment='邮箱地址')
    phone: Mapped[str] = mapped_column(String(20), nullable=True, comment='手机号码')
    avatar: Mapped[str] = mapped_column(String(255), nullable=True, comment='头像URL')
    api_key: Mapped[str] = mapped_column(String(128), nullable=False, unique=True, comment='API密钥')
    api_secret: Mapped[str] = mapped_column(String(128), nullable=False, comment='API密钥密码')
    client_type: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=1, comment='客户端类型：1-个人, 2-企业, 3-开发者')
    plan_type: Mapped[str] = mapped_column(String(20), nullable=False, default='free', comment='计划类型：free-免费版, basic-基础版, pro-专业版, enterprise-企业版')
    quota_monthly: Mapped[int] = mapped_column(Integer, nullable=False, default=100, comment='月度配额（分钟数）')
    quota_used_monthly: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment='月度已使用配额')
    quota_reset_date: Mapped[date] = mapped_column(Date, nullable=False, comment='配额重置日期')
    rate_limit_per_minute: Mapped[int] = mapped_column(Integer, nullable=False, default=10, comment='每分钟请求限制')
    allowed_models: Mapped[dict] = mapped_column(JSON, nullable=False, default=lambda: ["sensevoice-small"], comment='允许使用的模型列表')
    allowed_features: Mapped[dict] = mapped_column(JSON, nullable=False, default=lambda: {"upload": True, "realtime": False, "batch": False}, comment='允许使用的功能')
    ip_whitelist: Mapped[dict] = mapped_column(JSON, nullable=True, comment='IP白名单')
    webhook_url: Mapped[str] = mapped_column(String(500), nullable=True, comment='Webhook回调地址')
    webhook_secret: Mapped[str] = mapped_column(String(128), nullable=True, comment='Webhook密钥')
    organization: Mapped[str] = mapped_column(String(100), nullable=True, comment='组织机构')
    contact_person: Mapped[str] = mapped_column(String(50), nullable=True, comment='联系人')
    business_type: Mapped[str] = mapped_column(String(50), nullable=True, comment='业务类型')
    status: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=1, comment='状态：0-禁用, 1-启用, 2-冻结')
    is_verified: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=0, comment='是否已验证：0-未验证, 1-已验证')
    verification_token: Mapped[str] = mapped_column(String(128), nullable=True, comment='验证令牌')
    last_login_time: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment='最后登录时间')
    last_login_ip: Mapped[str] = mapped_column(String(45), nullable=True, comment='最后登录IP')
    password_hash: Mapped[str] = mapped_column(String(255), nullable=True, comment='密码哈希（可选）')
    password_reset_token: Mapped[str] = mapped_column(String(128), nullable=True, comment='密码重置令牌')
    password_reset_expires: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment='密码重置令牌过期时间')
    created_by: Mapped[int] = mapped_column(Integer, nullable=True, comment='创建者（系统管理员ID）')
    create_time: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_by: Mapped[int] = mapped_column(Integer, nullable=True, comment='更新者ID')
    update_time: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    del_flag: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=0, comment='删除标志：0-未删除, 1-已删除')
    remark: Mapped[str] = mapped_column(String(500), nullable=True, comment='备注')

    # 创建索引
    __table_args__ = (
        Index('idx_speech_client_user_username', 'username'),
        Index('idx_speech_client_user_email', 'email'),
        Index('idx_speech_client_user_api_key', 'api_key'),
        Index('idx_speech_client_user_status', 'status'),
        Index('idx_speech_client_user_client_type', 'client_type'),
        Index('idx_speech_client_user_plan_type', 'plan_type'),
        Index('idx_speech_client_user_create_time', 'create_time'),
        Index('idx_speech_client_user_del_flag', 'del_flag'),
        # 唯一约束（软删除情况下）
        Index('uk_speech_client_user_username', 'username', unique=True, postgresql_where='del_flag = 0'),
        Index('uk_speech_client_user_email', 'email', unique=True, postgresql_where='del_flag = 0'),
    ) 